using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Sedna.Core.DataBinding;
using Sedna.Core.Tiles.Contents;
using Spring.Util;

namespace Sedna.Core.Controls
{
	public class RegularExpressionValidator : System.Web.UI.WebControls.RegularExpressionValidator, IContent, IConfigurationAware
	{
		private IConfiguration configuration;
		private string configurationId;
        private string validationExpressionKey;

		private string textKey;
		private string errorMessageKey;

		private String contentPlaceHolderId;
        protected IDataBinder binder;

	    public string ValidationExpressionKey
	    {
	        get { return validationExpressionKey; }
	        set { validationExpressionKey = value; }
	    }

	    public String ContentPlaceHolderId
		{
			get { return contentPlaceHolderId;}
			set { contentPlaceHolderId = value;}
		}

		public IConfiguration Configuration
		{
			get { return configuration; }
			set { configuration = value; }
		}

		[Bindable(true), Category("Sedna"), DefaultValue("configuration.default.RegularExpressionValidator")]
		public string ConfigurationId
		{
			get { return configurationId; }
			set { configurationId = value; }
		}

		public string TextKey
		{
			get { return textKey; }
			set { textKey = value; }
		}

		public string ErrorMessageKey
		{
			get { return errorMessageKey; }
			set { errorMessageKey = value; }
		}

		public RegularExpressionValidator()
		{
			configurationId = "configuration.default.RegularExpressionValidator";
		}

		public RegularExpressionValidator(IConfiguration configuration) : this()
		{
			this.configuration = configuration;
			BuildControlFromConfiguration();
		}

		protected void BuildControlFromConfiguration()
		{
			ID=Configuration.Id;
			
			TextKey = configuration.TextKey;

            ValidationGroup = configuration.ValidationGroup;
	
			IRegularExpressionValidatorConfiguration validatorConfiguration = Configuration as IRegularExpressionValidatorConfiguration;
			if (validatorConfiguration == null)
			{
				return;
			}

			ControlToValidate = validatorConfiguration.ControlToValidate;
			ErrorMessageKey = validatorConfiguration.ErrorMessageKey;
			ValidationExpression = validatorConfiguration.ValidationExpression;
			Display = validatorConfiguration.Display;
		}

		protected override void OnInit(EventArgs e)
		{
			if (Configuration == null)
			{
				Configuration = (IConfiguration)SpringContextFactory.GetContext().GetObject(configurationId);
			}
            binder = new Sedna.Core.DataBinding.DataBinder(this, configuration, ExecutionContext.GetCurrent(ClientID));
            BuildControlFromConfiguration();
			base.OnInit (e);
		}

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Unbind();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Bind();
            Text = configuration.GetLocalizedPostfixLabel(TextKey, Text);
            ErrorMessage = configuration.GetLocalizedPostfixLabel(ErrorMessageKey, ErrorMessage);
            ValidationExpression = Configuration.GetLocalizedPostfixLabel(ValidationExpressionKey, ValidationExpression); 
        }
        
        protected virtual void Bind()
        {
            binder.Bind("Enabled");
        }

        protected virtual void Unbind()
        {
            binder.Unbind("Enabled");
        }

	}
}
